import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken,
				getUserId,setUserId,removeUserId,
				getProvince,setProvince,removeProvince,
				getCity,setCity,removeCity,
				getArea,setArea,removeArea,
				getAreaArr,setAreaArr,removeAreaArr,
				getImgUrl,setImgUrl,removeImgUrl } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    agencyUserId:getUserId(),
    agencyProvince:getProvince(),
    agencyCity:getCity(),
    agencyArea:getArea(),    
    agencyAreaArr:getAreaArr(),
    agencyImgUrl:getImgUrl(),
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
   /* SET_USERID: (state, userId) => {
      state.agencyUserId = userId
    },
    SET_IMGURL: (state, imgUrl) => {
      state.agencyImgUrl = imgUrl
    },*/
    SET_PROVINCE: (state, province) => {
      state.agencyProvince = province
    },
    SET_CITY: (state, city) => {
      state.agencyCity = city
    },
    SET_AREA: (state, area) => {
      state.agencyArea = area
    },
    SET_AREAARR: (state, areaArr) => {
      state.agencyAreaArr = areaArr
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password).then(response => {
          const data = response.data                 
          setToken(data.token)
          setProvince(data.province)
          setCity(data.city)
          setAreaArr(data.area)
          setArea(data.area[0])             
          commit('SET_TOKEN', data.token)
          commit('SET_PROVINCE', data.province)
          commit('SET_CITY', data.city)  
          commit('SET_AREAARR', data.area)  
          commit('SET_AREA', data.area[0]) 
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      /*return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          const data = response.data
          console.log(data)
          if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', data.roles)
          } else {
            reject('getInfo: roles must be a non-null array !')
          }
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })*/
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
      	  commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PROVINCE', '')
          commit('SET_CITY', '') 
          commit('SET_AREAARR', []) 
          commit('SET_AREA', '') 
          removeToken()
          removeProvince()
          removeCity()
          removeAreaArr()
          removeArea()
          resolve()
        /*logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_USERID', '')
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })*/
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_PROVINCE', '')
        commit('SET_CITY', '') 
        commit('SET_AREAARR', []) 
        removeToken()
        removeProvince()
        removeCity()
        removeAreaArr()
        removeArea()
        resolve()
      })
    }
  }
}

export default user
